/****<pre>
 * <select name="pattern" class="form-control input-sm" id="${id}pattern">
	<option class="null" value="">请选择</option>
	</select>
	 格式选择列表
	  config={
	  	id:id,
	  	onDataFormatChange:function(curDataType,curDataFormat){}
	  } 
 *</pre>
 */
define(function(require, exports, module) {
	
	function FormatSelector(config)
	{
		this.curDataFormat=undefined;	//当前字段格式
		this.curDataType=undefined;		//当前字段类型
		$.extend(true,this,config);
		this.$selector=$('#'+config.id);
		this.init();
	}
	FormatSelector.prototype={
		init:function(){
			var that=this;
			if(this.editable)
			{
				this.$selector.on('click',function(){
					that.curDataFormat=this.value;
					that.onDataFormatChange&&that.onDataFormatChange(that.curDataType,that.curDataFormat);
				});
			}
			else
			{
				$('option:first-child',this.$selector).html('');
				this.disable();
			}
		},
		enable:function(){
			this.editable&&this.$selector.removeAttr("disabled");
		},
		disable:function(){
			this.$selector.attr("disabled", "disabled");
		},
		show:function(type,format){//show(字段类型，字段格式A)
			//0)清空选项
			$('option[class!=null]',this.$selector).remove();
			this.curDataType=type;
			var that=this;
			//1)根据字段类型下载 字段格式列表
			$.ajax(this.baseUrl+"/dict/getTypeFormatDictItems.jhtml?type="+type,{
					type:'GET',
					dataType:'json'
				}).done(function(data){ 
					//2)填充option
					var html=[];
					$.each(data,function(index,ele){
						html.push('<option value="'+ele.itemValue+'">'+ele.itemName+'</option>');
					});
					that.$selector.append(html.join(''));
					
					//3)，并选中A，没有则把当前值置空 */
					if(!format)
						return;
					
					var curOption=$('option[value="'+format+'"]',that.$selector);
					if(curOption.length==0)
					{
						that.curDataFormat=undefined;
						that.onDataFormatChange&&that.onDataFormatChange(that.curDataType,that.curDataFormat);
					}	
					else
						curOption.prop("selected",true);
				}).fail(function(){
				});
		}
	};
	window["FormatSelector"]=FormatSelector;
});